<?php
import( 'de.ceus-media.framework.krypton.core.DefinitionAction' );
import( 'de.ceus-media.framework.krypton.exception.IO' );
import( 'classes.action.panel.ProjectSelector' );
import( 'classes.logic.Project' );
import( 'classes.logic.Server' );
import( 'classes.logic.ServerProject' );
/**
 *	Option Actions.
 *	@package		mv2.action.administration
 *	@extends		Framework_Krypton_Core_DefinitionAction
 *	@uses			Logic_Licencee
 *	@uses			Action_Panel_PlatformSelector
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
/**
 *	Option Actions.
 *	@package		mv2.action.administration
 *	@extends		Framework_Krypton_Core_DefinitionAction
 *	@uses			Logic_Licencee
 *	@uses			Action_Panel_PlatformSelector
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
class Action_Administration_Project extends Framework_Krypton_Core_DefinitionAction
{
	/**
	 *	Constructor.
	 *	@access		public
	 *	@return		void
	 */
	public function __construct()
	{
		parent::__construct();
		$this->loadLanguage( 'administration.project', 'project' );
		$this->addAction( 'addProject' );
		$this->addAction( 'editProject' );
		$this->addAction( 'removeProject' );
		$this->addAction( 'addServers' );
		$this->addAction( 'removeServers' );
		$action	= new Action_Panel_ProjectSelector();
		$action->performActions();
	}
	
	protected function addProject()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$words		= $this->words['project']['msg'];

		try
		{
			$logic		= new Logic_Project();
			$request->set( 'add_project_project_id', $request->get( 'projectId' ) );
			if( !$logic->addProject( $request->getAll(), 'add_project_' ) )
				return $this->messenger->noteError( $words['error_project_not_added'] );
			$this->messenger->noteSuccess( $words['success_project_added'] );
			$request->remove( 'add_project_title' );
			$request->remove( 'add_project_description' );
		}
		catch( Exception $e )
		{
			$this->handleException( $e, 'project', 'addProject' );
		}
	}
	
	protected function addServers()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$words		= $this->words['project']['msg'];

		$projectId	= $request->get( 'projectId' );
		$servers	= $request->get( 'add_servers' );
		if( $servers )
		{
			try
			{
				foreach( $servers as $serverId )
				{
					Logic_ServerProject::addProjectToServer( $projectId, $serverId );
				}
			}
			catch( Exception $e )
			{
				$this->handleException( $e, 'project', 'assignServers' );
			}
		}
	}
	
	protected function editProject()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$words		= $this->words['project']['msg'];

		$projectId		= $request->get( 'projectId' );
		if( $projectId )
		{
			try
			{
				$logic	= new Logic_Project();
				if( $logic->editProject( $projectId, $request->getAll(), 'edit_project_' ) )
					$this->messenger->noteSuccess( $words['success_project_edited'] );
				else
					$this->messenger->noteError( $words['error_project_not_edited'] );
				$request->remove( 'projectId' );
			}
			catch( Exception $e )
			{
				$this->handleException( $e, 'project', 'editProject' );
			}
		}		
	}

	protected function removeProject( $projectId )
	{
	}

	protected function removeServers()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( "request" );
		$words		= $this->words['project']['msg'];

		$projectId	= $request->get( 'projectId' );
		$servers	= $request->get( 'remove_servers' );
		if( $servers )
		{
			try
			{
				foreach( $servers as $serverId )
				{
					Logic_ServerProject::removeProjectFromServer( $projectId, $serverId );
				}
			}
			catch( Exception $e )
			{
				$this->handleException( $e, 'project', 'assignServers' );
			}
		}
	}
}
?>